$(function(){
    var ygkmdmys = {
        grid:$("#ygkmdmys_grid"),
        gridOptions:{
            url:$.webPath() + "/cwgl/kmys/ygkmdmys/ygkmdmysList",
//            updateUrl:$.webPath() +'/cwgl/kmys/ygkmdmys/save',
            toolbar:'#toolbar_ygkmdmys',
            fitColumns:true,
            fit:true,
            pagination:true,
            rownumbers:true,
            pageNumber: 1,
            singleSelect:true,
            columns:[
                [
                    {field:'yg.xm',title:'姓名',width:100},
                    {field:'zggsQc',title:'主管公司',width:100},
                    {field:'zgjgJc',title:'主管机构',width:100},
                    {field:'yg.kmdm',title:"辅助核算代码",editor:{type:'text'},width:100},
                    {field:'cz',title:'操作',formatter:function(value,row,index){
                        if(value && value == 'save')
                            return '<a index="'+index+'" name="save" href="javascript:void(0)" onclick="save_ygkmdmys('+index+')"></a>'
                        return '<a index="'+index+'" name="edit" href="javascript:void(0)" onclick="edit_ygkmdmys('+index+')"></a>'
                    },width:30,align:'center'}
                ]
            ],
            onLoadSuccess:function(){
                $("a[name='edit']").linkbutton({
                    plain:true,
                    iconCls:'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onBeforeEdit:function(rowIndex, rowData){
                ygkmdmys.grid.edatagrid("updateRow",{
                    index: rowIndex,
                    row:$.extend({},rowData,{cz:'save'})

                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onAfterEdit: function (index, row) {
                var url = $.webPath() +'/cwgl/kmys/ygkmdmys/save_ygkmdmys';
                $.post(url, row, function (data) {
                    if(data.statusCode == 200){
                        ygkmdmys.grid.datagrid('updateRow', {
                            index: index,
                            row:$.extend({},row,{cz:'edit'})
                        });
                        $("a[name='edit']").linkbutton({
                            plain:true,
                            iconCls:'icon-edit'
                        });
                    } else {
                        $.messager.alert('info',data.message);
                        ygkmdmys.grid.edatagrid("beginEdit",index);
                    }
                }, 'json');
            }
        },
//        save:function(){},
        search:function(){
            ygkmdmys.grid.datagrid("load",{
                zgjgId:$("#ygkmdmys_zgjg").combobox("getValue") || '',
                xm:$("#ygkmdmys_xm").val() || ''
//                kmdm:$("#ygkmdmys_kmdm").val() || ''
            });
        }
    }
    ygkmdmys.grid.edatagrid(ygkmdmys.gridOptions);
    $("#ygkmdmys_zgjg").gsjgtree(2);
//    $("#ygkmdmys_qyjg").qyjgcombobox(false);
    $("#btn_ygkmdmys_search").click(function(){
        ygkmdmys.search();
    });
});
function save_ygkmdmys(index){
    $("#ygkmdmys_grid").edatagrid("endEdit",index);
}
function edit_ygkmdmys(index){
    $("#ygkmdmys_grid").edatagrid("beginEdit",index);
}